<?php
/**
 * Created by PhpStorm.
 * User: phpzh
 * Date: 2020/6/6
 * Time: 11:30
 */

namespace app\api\controller\v1;


use app\api\controller\Common;
use app\common\lib\exception\ApiException;
use think\Log;

class Index extends Common
{
    /**
     * 获取首页接口
     * 1、头图 4-6
     * 2、推荐位列表 默认40条
     */
    public function index()
    {

        $heads = model('News')->getIndexHeadNormalNews();

        $heads = $this->getDealNews($heads);
        $positions = model('News')->getPositionNormalNews();
        $positions = $this->getDealNews($positions);

        $result = [
            'heads' => $heads,
            'positions' => $positions
        ];

        return show(config('code.success'), 'OK', $result, 200);

    }

    /**
     * 客户端初始化接口
     * 1、检测app是否需要升级
     */
    public function init()
    {
        //app_type 去ent_version查询
        $version = model('Version')->getLastVersionAppType($this->headers['app_type']);
        if (empty($version)) {
            return new ApiException('error', 400);
        }
        if ($version->version > $this->headers['version']) {
            $version->is_update = $version->is_force == 1 ? 2 : 1;
        } else {
            $version->is_update = 0;
        }
        //记录用户的基本信息
        $actives = [
            'version' => $this->headers['version'],
            'app_type' => $this->headers['app_type'],
            'did' => $this->headers['did'],
        ];
        try{
            model('AppActive')->add($actives);
        }catch (\Exception $E){
//           Log::write()
        }

        return show(config('config.success'), 'OK', $version, 200);


    }
}